package com.nbjtjc.safe.activiti.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.nbjtjc.safe.activiti.mapper.ExtActTaskReassignMapper;
import com.nbjtjc.safe.activiti.model.ExtActTaskReassign;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.sys.SysUserMapper;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @auther guqianbin
 * @date 2018/7/21 8:29
 * 重新改派task的类
 **/
@Service
public class ExtActTaskReassignService {

    @Autowired
    private ExtActTaskReassignMapper extActTaskReassignMapper;
    @Autowired
    private TaskService taskService;

    @Autowired
    private SysUserMapper sysUserMapper;

    public Result reassignReturn(ExtActTaskReassign extActTaskReassign) {
        Result result = new Result();
        Task task = taskService.createTaskQuery().taskId(extActTaskReassign.getTaskId()).singleResult();
        if(ObjectUtil.isNull(task)){
            result.setFalse("任务不存在");
            return result;
        }
        taskService.setAssignee(task.getId(),extActTaskReassign.getFromUserId().toString());
        extActTaskReassign.setHasReturn(true);
        extActTaskReassign.setOperationTime(new Date());
        extActTaskReassignMapper.updateByPrimaryKeySelective(extActTaskReassign);
        return result;
    }

    public ExtActTaskReassign findLatestReassign(String taskId) {
        ExtActTaskReassign extActTaskReassign = extActTaskReassignMapper.findByTaskId(taskId);
        return extActTaskReassign;
    }

    @Transactional
    public Result reassignTask(ExtActTaskReassign extActTaskReassign) {
        Result result = new Result();
        Task task = taskService.createTaskQuery().taskId(extActTaskReassign.getTaskId()).singleResult();
//        checkAndCreatReassign(taskId);
        ExtActTaskReassign addReassign = new ExtActTaskReassign();
        addReassign.setTaskId(extActTaskReassign.getTaskId());
        addReassign.setAssignUserId(extActTaskReassign.getAssignUserId());
        addReassign.setAssignUsername(sysUserMapper.findNameByUserId(extActTaskReassign.getAssignUserId()));
        addReassign.setFromUserId(Integer.parseInt(task.getAssignee()));
        addReassign.setFromUsername(sysUserMapper.findNameByUserId(Integer.parseInt(task.getAssignee())));
        addReassign.setCreateTime(new Date());
        extActTaskReassignMapper.insertSelective(addReassign);
        taskService.setAssignee(extActTaskReassign.getTaskId()
                , extActTaskReassign.getAssignUserId().toString());
        return result;
    }

    public List<ExtActTaskReassign> find(String taskId) {
        ExtActTaskReassign extActTaskReassign = new ExtActTaskReassign();
        extActTaskReassign.setTaskId(taskId);
        List<ExtActTaskReassign> extActTaskReassignList = extActTaskReassignMapper.select(extActTaskReassign);
        return extActTaskReassignList;
    }

    /**
     * 如果该任务是第一次改派，在改派前创建一个
     * 记录保存任务最初人员
     */
    private void checkAndCreatReassign(String taskId) {
        ExtActTaskReassign temp = new ExtActTaskReassign();
        temp.setTaskId(taskId);
        List<ExtActTaskReassign> extActTaskReassignList = extActTaskReassignMapper.select(temp);
        if (CollectionUtil.isEmpty(extActTaskReassignList)) {
            Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
            ExtActTaskReassign extActTaskReassign = new ExtActTaskReassign();
            extActTaskReassign.setAssignUserId(Integer.parseInt(task.getAssignee()));
            extActTaskReassign.setCreateTime(new Date());
            extActTaskReassign.setHasReturn(false);
            extActTaskReassignMapper.insertSelective(extActTaskReassign);
        }
    }
}
